﻿
package com.ostrich.project.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.page.Pager;
import com.base.util.DateUtil;
import com.base.util.HtmlUtil;
import com.base.util.SessionUtils;
import com.base.web.BaseAction;
import com.ostrich.manpower.common.FormulaUtil;
import com.ostrich.project.bean.TostPjCostitem;
import com.ostrich.project.bean.TostPjProjcttab;
import com.ostrich.project.service.impl.TostPjCostitemService;
import com.ostrich.project.service.impl.TostPjProjcttabService;
import com.ostrich.system.analysis.ParaminfoUtil;
import com.ostrich.system.bean.TostSysParaminfo;

/**
 * ClassName: Controller <br/>
 * date: 2013-5-9 下午03:07:38 <br/>
 * 
 * @author wangyinan
 * @version
 */
@Controller
@RequestMapping("/tostPjCostitem")
public class TostPjCostitemController extends BaseAction
{
	private final static Logger log= Logger.getLogger(TostPjCostitemController.class);

	// Servrice start
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostPjCostitemService<TostPjCostitem> tostPjCostitemService; 
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostPjProjcttabService<TostPjProjcttab> tostpjprojcttabservice; 
	/**
	 * ilook 首页
	 * @param url
	 * @param classifyId
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView  list(TostPjCostitem tostPjCostitem,HttpServletRequest request) throws Exception{
		Map<String,Object>  context = getRootMap();
		Map<Integer, TostSysParaminfo> cmps = ParaminfoUtil.getParamByPid(60);
		
		TostPjProjcttab pjtab = tostpjprojcttabservice.queryById(tostPjCostitem.getPjId());
		request.getSession().setAttribute("PjId",pjtab);
		//不懂意思？？
		List<TostPjCostitem> dataList = tostPjCostitemService.queryByList(tostPjCostitem);
		//设置页面数据
		context.put("cmps", cmps);
		context.put("pjtab", pjtab);
		context.put("list", dataList);
		return forword("project/tabTostPjCostitem",context); 
	}
	
	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/save")
	public ModelAndView save(TostPjCostitem bean,HttpServletResponse response,HttpServletRequest request) throws Exception{
		
		bean.setCreatetime(DateUtil.getNowPlusTime());
		bean.setPerson(SessionUtils.getUser(request).getEmail());
		
		if (bean.getId() == null) {
			tostPjCostitemService.addCost(bean);
		} else {
			tostPjCostitemService.update(bean);
		}
		
		//获取分类参数信息 条转url连接地址
		Map<Integer, TostSysParaminfo> cmpurl = ParaminfoUtil.getParamByPid(60);
		String url = cmpurl.get(bean.getPctType()).getPaValue();
		Map<String,Object>  context = getRootMap();

		context.put("pjId", bean.getPjId());
		context.put("pctId", bean.getId());
		return new ModelAndView("redirect:"+url,context);
	}
	
	/**
	 * json 列表页面
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/dataList") 
	public void  dataList(TostPjCostitem tostPjCostitem,HttpServletResponse response) throws Exception{
	
		
		Pager pager = new Pager();
		pager.setPageSize(1000);
		tostPjCostitem.setPager(pager);
        List<TostPjCostitem> dataList = tostPjCostitemService.queryByList(tostPjCostitem);
		//设置页面数据
        Map<String,Object> jsonMap = new HashMap<String,Object>();
		
		jsonMap.put("total",(dataList!=null)?dataList.size():0);
		jsonMap.put("rows", dataList);
		HtmlUtil.writerJson(response, jsonMap);
	}
	
	@RequestMapping("/getId")
	public void getId(Integer id,HttpServletResponse response) throws Exception{
		Map<String,Object>  context = getRootMap();
		TostPjCostitem bean  = tostPjCostitemService.queryById(id);
		if(bean  == null){
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", bean);
		HtmlUtil.writerJson(response, context);
	}
	
	@RequestMapping("/delete")
	public void delete(Integer[] id,HttpServletResponse response) throws Exception{
		tostPjCostitemService.delete(id);
		sendSuccessMessage(response, "删除成功");
	}
	
	@RequestMapping("/deleteByid")
	public void deleteByid(TostPjCostitem tostPjCostitem,HttpServletResponse response) throws Exception{
		tostPjCostitemService.deleteByid(tostPjCostitem);
		sendSuccessMessage(response, "删除成功");
	}
	
	@RequestMapping("/setPubsupport")
	public void setPubsupport(TostPjCostitem pjcost,HttpServletResponse response) throws Exception{
		//System.out.println(pjcost);
		Map<String,Object> result = tostPjCostitemService.getPjTotalByPjId(pjcost.getPjId());
		boolean isExist= false;
		isExist = (result!=null)?(Boolean)result.get("isExist"):false;
		if(pjcost.getPctType()==66)
			sendSuccessMessage(response, (!isExist)?"您的预算还缺少项目提成、或者非现金支出（提成职员）、或者工程人工费用，请点击取消，返回添加相应的其他预算类型，或者继续将其他没有预算类型为零计算。确认继续或取消返回。":"您的公共支撑部门费用分摊可以继续下一步进行系统自动生成，请点击确定继续。");
		else if(pjcost.getPctType()==67)
			sendSuccessMessage(response, (!isExist)?"您的预算还缺少项目提成、或者非现金支出（提成职员）、或者工程人工费用，请点击取消，返回添加相应的其他预算类型，或者继续将其他没有预算类型为零计算。确认继续或取消返回。":"您的税金费用可以继续下一步进行系统自动生成，请点击确定继续。");
		else if(pjcost.getPctType()==65)
			sendSuccessMessage(response, (!isExist)?"您的预算还缺少项目提成、或者工程人工费用，请点击取消，返回添加相应的其他预算类型，或者继续将其他没有预算类型为零计算。确认继续或取消返回。":"您的其他（工会经费）费用可以继续下一步进行系统自动生成，请点击确定继续。");
	}
	
	@RequestMapping("/addPubsupport")
	public void addPubsupport(TostPjCostitem pjcost,HttpServletResponse response,HttpServletRequest request) throws Exception{
		tostPjCostitemService.addCostitem(pjcost, request,pjcost.getPctType());
		if(pjcost.getPctType()==66)
			sendSuccessMessage(response, "“公共支撑部门费用分摊”已经成功重新生成了！");
		else if(pjcost.getPctType()==67)
			sendSuccessMessage(response, "“税金”已经成功重新生成了！");
		else if(pjcost.getPctType()==65)
			sendSuccessMessage(response, "“其他（公会经费）”已经成功重新生成了！");
	}
	/****
	 * 添加 工会经费
	 * @param pjcost
	 * @param response
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addLaborunion")
	public ModelAndView addLaborunion(TostPjCostitem pjcost,HttpServletResponse response,HttpServletRequest request) throws Exception{
		Map<String, Object> context = getRootMap();
		
		Map<String,Object> sumnub = FormulaUtil.formulaById(26, pjcost.getPjId());
		Float budget = (sumnub!=null && sumnub.get("PCT_BUDGET")!=null)?(Float)sumnub.get("PCT_BUDGET"):new Float(0);
		Float praise = (sumnub!=null && sumnub.get("PCT_PRAISE")!=null)?(Float)sumnub.get("PCT_PRAISE"):new Float(0);
		Float total = (sumnub!=null && sumnub.get("PCT_TOTAL")!=null)?(Float)sumnub.get("PCT_TOTAL"):new Float(0);
		
		budget=(float) (budget*0.057);
		praise=(float) (praise*0.057);
		total=(float) (total*0.057);
		
		pjcost.setPctBudget(budget);
		pjcost.setPctPraise(praise);
		pjcost.setPctTotal(total);
		pjcost.setPctName("税金");
		pjcost.setPctType(66);

		List<TostPjCostitem> dataList = tostPjCostitemService.queryByList(pjcost);
		if(dataList==null || dataList.size()<=0){
			pjcost.setCreatetime(DateUtil.getNowPlusTime());
			pjcost.setPerson(SessionUtils.getUser(request).getEmail());
			tostPjCostitemService.add(pjcost);
		}else{
			TostPjCostitem tp = dataList.get(0);
			pjcost.setId(tp.getId());
			tostPjCostitemService.updateBySelective(pjcost);
		}
		return forword("penmsg", context);
	}
	
	
	//查询参加项目的部门；
	@RequestMapping("/querycostitembypjid") 
		public void  querycostitembypjid(TostPjCostitem pjcost,HttpServletResponse response) throws Exception{
		List<TostPjCostitem> dataList = tostPjCostitemService.queryByList(pjcost);
		List<TostPjCostitem> sx = new ArrayList<TostPjCostitem>();
		for(TostPjCostitem pcs:dataList){
			//查询出现金提成和工程技术人员提成类型
			if(pcs.getPctType()==61 || pcs.getPctType()==64 )
				sx.add(pcs);
		}
			//设置页面数据
			Map<String,Object> jsonMap = new HashMap<String,Object>();
			jsonMap.put("total",(sx!=null)?sx.size():0);
		    jsonMap.put("rows", sx);
		    HtmlUtil.writerJson(response, jsonMap);
		}
	
}
